package com.fit.dao.client;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.Client;
import com.fit.entities.LicencePlate;

public class ClientDAOImpl implements ClientDAO {

	@Inject
	private Session session;

	public boolean save(Client client) {
		if (client != null) {
			session.save(client);
			return true;
		}
		return false;
	}

	public Client findByPersonalId(String personalId) {
		ArrayList<Client> list = (ArrayList<Client>) session.createCriteria(Client.class).add(Restrictions.eq("personalId",personalId)).list();
		if(list.size()==0){
			return null;
		} 
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Client> findAll() {
		return session.createCriteria(Client.class).list();
	}

	public boolean update(Client client) {
		if (client != null) {
			session.update(client);
			return true;
		}
		return false;
	}

	public boolean deleteByPersonalId(String personalId) {
		Client c = findByPersonalId(personalId);
		if (c != null) {
			session.delete(c);
			return true;
		}
		return false;
	}

	public boolean checkRegistrations(Client client) {
		ArrayList<LicencePlate> list = (ArrayList<LicencePlate>) session.createCriteria(LicencePlate.class).add(Restrictions.eq("client", client)).list();
		if(list.size()!=0){
			return true;
		}
		return false;
	}

}
